package uz.rt.physics;

import uz.rt.units.Energy;
import uz.rt.units.Length;
import uz.rt.utils.Pair;

public class Pdd extends Curve<Length, Double>
{
// ------------------------------ FIELDS ------------------------------

    /**
     * Energy (MV / MeV) at which the PDD was measured
     */
    private Energy energy = null;

// --------------------- GETTER / SETTER METHODS ---------------------

    /**
     * Get the energy at which the PDD was measured
     *
     * @return Energy (MV / MeV)
     */
    public Energy getEnergy()
    {
        return energy;
    }

    /**
     * Set the energy at which the PDD was measured
     *
     * @param energy Energy (MV / MeV)
     */
    public void setEnergy(final Energy energy)
    {
        this.energy = energy;
    }

// -------------------------- OTHER METHODS --------------------------

    /**
     * Scale the maximum value of the curve to 100% making any curve a PDD
     */
    public void scale()
    {
        double max = Double.MIN_VALUE;
        for (Pair<Length, Double> anA : a)
        {
            if (anA.getY() > max) max = anA.getY();
        }

        for (Pair<Length, Double> anA : a)
        {
            double tmp = anA.getY() / max * 100.0;
            anA.setY(tmp);
        }
    }

    @Override
    public boolean equals(Object o)
    {
        if (this == o) return true;
        if (!(o instanceof Pdd)) return false;
        if (!super.equals(o)) return false;

        Pdd pdd = (Pdd) o;

        return energy.equals(pdd.energy);

    }

    @Override
    public int hashCode()
    {
        int result = super.hashCode();
        result = 31 * result + energy.hashCode();
        return result;
    }

    /**
     * Set all units of the x parameters equal
     *
     * @param unit Length unit
     */
    protected void equaliseScales(Length.Unit unit)
    {
        for (int i = 0; i < size(); i++)
        {
            at(i).getX().scale(unit);
        }
    }
}
